local Monomer = require("app.figure.Monomer")
local PlayerMoveQueue = require("app.figure.PlayerMoveQueue")
local MapPoint = require("app.map.MapPoint")
local BgMap = require("app.map.BgMap")
-- 玩家类
local PlayerController= class("PlayerController")

function PlayerController:ctor(data)
    self.mPlayer = nil
    self.mPlayerMoveQueue = nil
    self.nCurFrame = 60
    self.nMoveStep = 120 --pix/s   
    self.bCanMove = true
    self.bIsLeader = false --主角
    self:createPlayer()

    --self:startUpdatePlayerCtrl()
end

function PlayerController:createPlayer()
    self.mPlayer = Monomer.new()
    self.mPlayer:setFigure(TexturePathType.Figure,11001)
    self.mPlayerMoveQueue = PlayerMoveQueue.new(self)
    return self.mPlayer
end

function PlayerController:goTo(mpoint)
    if not self.bCanMove then
        return
    end
    local begin = nil
    --正在移动
    if self.mPlayerMoveQueue:getNextTarGrid() then
        begin = self.mPlayerMoveQueue:getNextTarGrid()
    else
        begin = self.mPlayer:getCurrGrid()
    end
    self.mPlayerMoveQueue:goto(MapController:getPath(begin, mpoint))
end

function PlayerController:forceTo(grid)
    self.mPlayerMoveQueue:stop()   
    self.mPlayer:setPosition(MapPoint.grid2ccp(grid))
    self.mPlayer:stand()
end

function PlayerController:setFigureState(state, direction,fraDirt)
    self.mPlayer:setFrameDirection(fraDirt)
    self.mPlayer:setFigureState(state, direction)   
end

function PlayerController:playAnimation(state,movedir)
    local direction = FigureDirection.Down
    local fraDirt = FigureFrameDirection.Down
    if movedir.x == 0 and movedir.y == 1 then
        direction = FigureDirection.Up
        fraDirt = FigureFrameDirection.Up
    elseif movedir.x == -1 and movedir.y == 1 then
        direction = FigureDirection.LeftAndUp
        fraDirt = (fraDirt==FigureFrameDirection.Left or fraDirt==FigureFrameDirection.Up) and fraDirt or FigureFrameDirection.Left
    elseif movedir.x == -1 and movedir.y == 0 then
        direction = FigureDirection.Left
        fraDirt = FigureFrameDirection.Left
    elseif movedir.x == -1 and movedir.y == -1 then
        direction = FigureDirection.LeftAndDown
        fraDirt = (fraDirt==FigureFrameDirection.Left or fraDirt==FigureFrameDirection.Down) and fraDirt or FigureFrameDirection.Left
    elseif movedir.x == 0 and movedir.y == -1 then
        direction = FigureDirection.Down
        fraDirt = FigureFrameDirection.Down
    elseif movedir.x == 1 and movedir.y == -1 then
        direction = FigureDirection.RightAndDown
        fraDirt = (fraDirt==FigureFrameDirection.Right or fraDirt==FigureFrameDirection.Down) and fraDirt or FigureFrameDirection.Right
    elseif movedir.x == 1 and movedir.y == 0 then
        direction = FigureDirection.Right
        fraDirt = FigureFrameDirection.Right
    elseif movedir.x == 1 and movedir.y == 1 then
        direction = FigureDirection.RightAndUp
        fraDirt = (fraDirt==FigureFrameDirection.Right or fraDirt==FigureFrameDirection.Up) and fraDirt or FigureFrameDirection.Right
    end
    self:setFigureState(state,direction,fraDirt)
end

function PlayerController:updatePlayerCtrl(deltaTime)
    if self.mPlayerMoveQueue then
        self.mPlayerMoveQueue:update(deltaTime)
    end
end
--抵达新格子
function PlayerController:arriveNextGrid()
    self:updateVertexZ()
    if self.bIsLeader then
        MapController:sendPlayerWalk(self.mPlayer:getCurrGrid())
        --检查切换地图
        MapController:detectReplaceMap(self.mPlayer:getCurrGrid())
    end
end

function PlayerController:updateVertexZ()
    local point = cc.p(self.mPlayer:getPosition())
    local value = BgMap.getZOrder(point) -- z轴
    self.mPlayer:setLocalZOrder(value)
        
    if (MapController:getCurMap():getCurrentGridValue(MapPoint.new(point)) == 2) then
        if (self.mPlayer:getOpacityEx() == 128) then
            return
        end
            
        self.mPlayer:setOpacityEx(128)
        self.mPlayer:setColor(cc.c3b(166,166,166))
    else
        if (self.mPlayer:getOpacityEx() == 255) then
            return
        end
            
        self.mPlayer:setOpacityEx(255)
        self.mPlayer:setColor(cc.c3b(255,255,255))
    end
end

-- 开启更新地图计时器
function PlayerController:startUpdatePlayerCtrl()
    self:stopUpdatePlayerCtrl()
    if not self.schedulePlayerCtrl then
		self.schedulePlayerCtrl = cc.Director:getInstance():getScheduler():scheduleScriptFunc(handler(self, self.updatePlayerCtrl), 1/self.nCurFrame, false)
	end
end

-- 关闭更新地图计时器
function PlayerController:stopUpdatePlayerCtrl()
    if self.schedulePlayerCtrl then
		cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulePlayerCtrl)
		self.schedulePlayerCtrl = nil
	end
end

function PlayerController:getCurrPlayer()
    return self.mPlayer
end

function PlayerController:getCurrLocation()
    local point = cc.p(self.mPlayer:getPosition())       
    return MapPoint.ccp2gridp(point)
end

function PlayerController:cleanUp()
    self.mPlayerMoveQueue = nil
    self:stopUpdatePlayerCtrl()
    if self.mPlayer then
        self.mPlayer:removeFromParent()
    end
end

return PlayerController